package com.young.web.ec.web.controller;

import java.util.UUID;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.young.web.ec.core.service.OrderService;
import com.young.web.ec.events.orders.CreateOrderEvent;
import com.young.web.ec.events.orders.OrderCreatedEvent;
import com.young.web.ec.events.orders.OrderDetails;
import com.young.web.ec.web.domain.Basket;
import com.young.web.ec.web.domain.CustomerInfo;

@Controller
@RequestMapping("/checkout")
public class CheckoutController {

	private static final Logger LOG = LoggerFactory.getLogger(BasketCommandController.class);
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private Basket basket;
	
	@RequestMapping(method = RequestMethod.GET)
	public String checkout() {
		return "/checkout";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String doCheckout(@Valid @ModelAttribute("customerInfo") CustomerInfo customer, BindingResult result, RedirectAttributes redirectAttrs) {
		if(result.hasErrors()){
			// errors in the form
            // show the checkout form again
			return "/checkout";
		}
		
		LOG.debug("No errors, continue with processing for Customer {}:",
                customer.getName());
		
		OrderDetails order = basket.createOrderDetailsWithCustomerInfo(customer);
		
		OrderCreatedEvent event = orderService.createOrder(new CreateOrderEvent(order));
		
		UUID key = event.getNewOrderKey();
		
		redirectAttrs.addFlashAttribute("message", "Your order has been accepted!");
		
		basket.clear();
		LOG.debug("Basket now has {} items", basket.getSize());
		
		return "redirect:/order/" + key.toString();
	}
	
	@ModelAttribute("customerInfo")
	private CustomerInfo getCustomerInfo() {
		return new CustomerInfo();
	}
	
	public void setBasket(Basket basket) {
		this.basket = basket;
	}
	
	public Basket getBasket() {
		return this.basket;
	}
}
